﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Xml.Linq;
using System.Net;
using StrategicDataSystems.Cloud.Monitoring.HostedServices.ResponseToEntityMappers;
using StrategicDataSystems.Cloud.Monitoring.HostedServices;

namespace StrategicDataSystems.Cloud.Monitoring.Tests.Given_A_OperationStatusResponseMapper
{
    [TestFixture]
    public class When_ConvertToEntity_Is_Called
    {
        [Test]
        public void Should_Return_A_Populated_OperationStatus_When_Not_Failed()
        {
             XDocument testResponse = XDocument.Parse(
               @"<?xml version='1.0' encoding='utf-8'?>
                  <Operation xmlns='http://schemas.microsoft.com/windowsazure'>
                    <ID>68DD8D8C-4807-4E4C-BA0C-75B63AF99F84</ID>
                    <Status>Succeeded</Status>
                  </Operation>");

            OperationStatusResponseMapper mapper = new OperationStatusResponseMapper();
            HostedServiceOperationStatus opStatus = mapper.MapToEntity(testResponse);

            Assert.AreEqual("Succeeded", opStatus.Status);
            Assert.AreEqual("68DD8D8C-4807-4E4C-BA0C-75B63AF99F84", opStatus.OperationId);
        }

        [Test]
        [ExpectedException(ExpectedException = typeof(WebException),
            ExpectedMessage = "Azure request ID:68DD8D8C-4807-4E4C-BA0C-75B63AF99F84 failed with error code: 121. Message: Test failure.")]
        public void Should_Return_An_Exception_When_Operation_Failed()
        {
            XDocument testResponse = XDocument.Parse(
               @"<?xml version='1.0' encoding='utf-8'?>
                  <Operation xmlns='http://schemas.microsoft.com/windowsazure'>
                    <ID>68DD8D8C-4807-4E4C-BA0C-75B63AF99F84</ID>
                    <Status>Failed</Status>
                    <Error>
                        <Code>121</Code>
                        <Message>Test failure.</Message>
                    </Error>
                  </Operation>");

            OperationStatusResponseMapper mapper = new OperationStatusResponseMapper();
            HostedServiceOperationStatus opStatus = mapper.MapToEntity(testResponse);
        }
    }
}
